PerfettoHelper: Store traces in /data/local/tmp instead of /sdcard On devices using HSUM (headless system user mode), the test runs as the current user, e.g. 10, but shell commands run as the system user 0. Since the test results are stored in /sdcard which is user-specific storage, moving the trace file to the test results directory using the mv command fails since /sdcard/test_results doesn't exist for user 0. So store the traces in /data/local/tmp instead, which is not user-specific. Bug: 382321446 Test: Check that perfetto traces are saved for various tests: power-open-fixed-calculator - regular android test http://android-build/abtd/run/L99900030009054008/ systemui-multiuser-suite - android multiuser test AndroidAutomotiveMultiuserScenarioTests - AAOS multiuser test http://android-build/abtd/run/L31000030009013452/ tradefed.dts.power-idle-display-on - desktop test (using HSUM) http://android-build/abtd/run/L37800030009025042/ Merged-In: I8d8d5de2eebf55f0715be6f7550a43300990c578 Change-Id: I8d8d5de2eebf55f0715be6f7550a43300990c578
diff --git a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java index a73d48d..b1394d9 100644 --- a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java +++ b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java
@@ -471,17 +471,17 @@ * @return true if the trace file copied successfully otherwise false. */ private boolean copyFileOutput(String destinationFile) { + // Create the destination directory if it doesn't already exist. Path path = Paths.get(destinationFile); String destDirectory = path.getParent().toString(); - // Check if the directory already exists - File directory = new File(destDirectory); - if (!directory.exists()) { - boolean success = directory.mkdirs(); - if (!success) { - Log.e(LOG_TAG, String.format( - "Result output directory %s not created successfully.", destDirectory)); - return false; - } + try { + mUIDevice.executeShellCommand(String.format("mkdir -p %s", destDirectory)); + } catch (IOException ioe) { + Log.e( + LOG_TAG, + String.format("Failed to create destination directory %s", destDirectory), + ioe); + return false; } // Copy the collected trace from /data/misc/perfetto-traces/trace_output.perfetto-trace to
diff --git a/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoTracingStrategy.java b/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoTracingStrategy.java index 06d4fb9..74979ca 100644 --- a/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoTracingStrategy.java +++ b/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoTracingStrategy.java
@@ -81,7 +81,7 @@ // Destination directory to save the trace results. private static final String TEST_OUTPUT_ROOT = "test_output_root"; // Default output folder to store the perfetto output traces. - private static final String DEFAULT_OUTPUT_ROOT = "/sdcard/test_results"; + private static final String DEFAULT_OUTPUT_ROOT = "/data/local/tmp/perfetto-traces"; // Default wait time before stopping the perfetto trace. private static final String DEFAULT_WAIT_TIME_MSECS = "0"; // Argument to get custom time in millisecs to wait before starting the
diff --git a/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java b/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java index e77ce4b..34c7e07 100644 --- a/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java +++ b/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java
@@ -155,7 +155,7 @@ .stopCollecting( anyLong(), eq( - "/sdcard/test_results/run_test1/" + "/data/local/tmp/perfetto-traces/run_test1/" + "PerfettoTracingPerTestStrategy/" + "perfetto_run_test1-1.perfetto-trace")); } @@ -567,7 +567,7 @@ .stopCollecting( anyLong(), eq( - "/sdcard/test_results/run#123_test1#456/" + "/data/local/tmp/perfetto-traces/run#123_test1#456/" + "PerfettoTracingPerTestStrategy/" + "perfetto_run#123_test1#456-1.perfetto-trace")); }